package com.pigcloud.sop.websiteserver.service.impl;

import com.alibaba.cloud.nacos.NacosServiceManager;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.pigcloud.sop.websiteserver.service.ServerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.PostConstruct;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Properties;
import java.util.stream.Collectors;

/**
 * @author wangfm
 */
@Slf4j
public class NacosServerService implements ServerService  {

    @Value("${nacos.discovery.server-addr:${spring.cloud.nacos.discovery.server-addr:}}")
    private String nacosAddr;

    @Value("${nacos.discovery.namespace:${spring.cloud.nacos.discovery.namespace:}}")
    private String nacosNamespace;

    @Value("${nacos.discovery.group:${spring.cloud.nacos.discovery.group:DEFAULT_GROUP}}")
    private String nacosGroup;
    @Value("${nacos.discovery.username:${spring.cloud.nacos.discovery.username:}}")
    private String username;
    @Value("${nacos.discovery.password:${spring.cloud.nacos.discovery.password:}}")
    private String password;
//    private NamingService namingService;
    @Autowired
    private NacosServiceManager nacosServiceManager;
//    @PostConstruct
//    public void after() throws NacosException {
//        if (StringUtils.isBlank(nacosAddr)) {
//            throw new IllegalArgumentException("请在配置文件中指定nacos.discovery.server-addr参数");
//        }
//        Properties nacosProperties = new Properties();
//        nacosProperties.put(PropertyKeyConst.SERVER_ADDR, nacosAddr);
//        if (StringUtils.isNotBlank(nacosNamespace)) {
//            nacosProperties.put(PropertyKeyConst.NAMESPACE, nacosNamespace);
//        }
//        if (StringUtils.isNotBlank(username)) {
//            nacosProperties.put(PropertyKeyConst.USERNAME, username);
//        }
//        if (StringUtils.isNotBlank(password)) {
//            nacosProperties.put(PropertyKeyConst.PASSWORD, password);
//        }
//        namingService = NamingFactory.createNamingService(nacosProperties);
//    }

    @Override
    public List<String> listServerHost(String serviceId) {
        Objects.requireNonNull(serviceId, "serviceId can not be null");
        List<String> list = Collections.emptyList();
        try {
            List<Instance> allInstances = nacosServiceManager.getNamingService().getAllInstances(serviceId, nacosGroup);
            list = allInstances.stream().map(instance -> instance.getIp() + ":" + instance.getPort())
                    .collect(Collectors.toList());
        } catch (NacosException e) {
            log.error("获取实例host出错, serviceId:{}", serviceId, e);
        }
        return list;
    }
}
